<?php include "./pc/view/header.inc.htm"; ?>

<div class="container">
    <div class="addtopic">
        <form method="POST" id="thread_create_form" action="thread-create.htm">
        <div class="content-header">
            <div class="tw-header-inner">
                <div class="select-box">
                    <span id="forum_select"></span>
                </div>
                <input class="form-control" type="text" placeholder="标题" name="subject" style="width:450px;">
            </div>
        </div>
        <div class="content-post">
            <div class="post-inner">
                <textarea class="input-body" id="message" name="message" placeholder="内容" style="width: 93%; height: 200px;"></textarea>
                <div class="post-button-left mt15">
                    <button class="Button Button--primary" type="submit" loading-text="正在提交...">
                        <span class="submit-button-text">
                            发表
                        </span>
                    </button>
                </div>
                <!-- <div class="post-button-right mt15"></div> -->
                <div class="clear"></div>
            </div>
        </div>
    </div>
</div>

<script id="script_data">
var fid = <?php echo $fid; ?>;// 当前激活的 fid
</script>

<?php include "./pc/view/footer.inc.htm"; ?>

<link rel="stylesheet" type="text/css" href="xneditor/editor.css" />

<script src="xiunoui/async.js"></script>
<script src="xiunoui/upload.js"></script>
<script src="xneditor/editor.js"></script>
<script>

var editor = new $.XNEditor({textarea: 'message', image_upload_url:'post-upload-1.htm', image_upload_width: <?php echo $conf['upload_image_width']; ?>, file_upload_url:'post-upload-0.htm'});
var jform = $('#thread_create_form');
var jinput = jform.find('input');
var jsubject = jinput.filter('[name="subject"]').focus();
var jmessage = $('#message');
var jseo_url = jform.find('input[name="seo_url"]');
var jsubmit = jform.find('button').filter('[type="submit"]');

// 选择论坛
var jforum_select = $('#forum_select');
var forumarr = <?php echo $forumarr; ?>;
var s = '<select name="fid">'; 
for(k in forumarr) s += '<option value="'+k+'">'+forumarr[k]+'</option>';
s += '</select>';
var jselect = jforum_select.html(s).find('select');
jselect.checked(fid);

jform.off('submit').on('submit', function() {
    if(!jsubject.val()) {
        jsubject.popover('请输入标题！').focus();
        return false;
    }
    if(!jmessage.val()) {
        editor.jeditor.popover('请输入内容');
        editor.focus();
        return false;
    }
    jsubmit.button('loading');
    var postdata = jform.serializeObj();
    $.xpost(jform.attr('action'), postdata, function(code, message) {
        if(code == 0) {
            jsubmit.button('发帖成功');
            var fid = $('select[name="fid"]').checked();
            setTimeout(function() {editor.clear(); window.location='forum-'+fid+'.htm';}, 1000);
            return;
        } else if(code == 1) {
            jsubject.popover(message).focus();
        } else if(code == 2) {
            jmessage.popover(message).focus();
        } else if(code == 3) {
            jselect.popover(message).focus();
        } else if(code == 4) {
            jseo_url.popover(message).focus();
        } else {
            $.alert(message);
        }
        jsubmit.button('reset');
    });
    return false;
});

// 快捷键回复
jform.keyup(function(e) {
    if((e.ctrlKey && (e.which == 13 || e.which == 10)) || (e.altKey && e.which == 83)) {
        jsubmit.trigger('click');
        return false;
    }
});

</script>